package com.wk.wanjetpack.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.wk.wanjetpack.R
import com.wk.wanjetpack.bean.Article

/**
 * @author peng
 * @date 2021/1/20
 * 说明
 */
class MainListAdapter : PagingDataAdapter<Article,MainListHolder>(DIFF_CALLBACK) {

    companion object{
        val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Article>(){
            override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean {
                return oldItem.id == newItem.id
            }

        }
    }

    override fun onBindViewHolder(holder: MainListHolder, position: Int) {
        holder.tvTitle.text = getItem(position)?.title?:"无标题"
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainListHolder {
        return MainListHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_article,parent,false))
    }
}


class MainListHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    val tvTitle = itemView.findViewById<TextView>(R.id.tv_title_article)
}